<template>
    <div class="News">

        <div class="tableBar">

            <div class="nav">
                <el-input v-model="input" placeholder="请输入资讯标题" style="width: 250px;margin-top: 15px;" clearable
                    @keyup.enter.native="handleQuery">
                    <i slot="prefix" class="el-input__icon el-icon-search" style="cursor: pointer" @click="handleQuery"></i>
                </el-input>
                <el-button style="height: 50px; margin-top: 10px; margin-right: 20px;" type="primary"
                    @click="addMemberHandle()">
                    + 添加资讯
                </el-button>

            </div>

            <el-table style="margin-top: 30px; width: 99%;" :data="tableData" stripe class="tableBox" :height="400">
                <el-table-column align="center" type="index" width="50"></el-table-column>
                <el-table-column align="center" prop="title" label="标题" width="120"></el-table-column>
                <el-table-column align="center" prop="content" label="内容" width="820"></el-table-column>
                <el-table-column align="center" prop="status" label="状态" width="80">
                    <template slot-scope="scope" align="center">
                        <span>{{ scope.row.status == '0' ? '未发布' : '已发布' }}</span>
                    </template>
                </el-table-column>


                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" size="medium" class="blueBug" @click="statusHandle(scope.row)">
                            {{ scope.row.status == '0' ? '发布' : '下架' }}
                        </el-button>
                        <el-button type="text" size="medium" class="blueBug" @click="update(scope.row)">
                            修改
                        </el-button>
                        <el-button type="text" size="medium" class="delBut non" @click="deleteHandle(scope.row)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination class="pageList" :page-sizes="[3, 6, 9, 12]" :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper" :total="counts" :current-page.sync="page"
                @size-change="handleSizeChange" @current-change="handleCurrentChange">
            </el-pagination>
        </div>


        <el-dialog :title="mytitle" :visible.sync="dialogVisible" width="60%" @close="resetForm('news')">
            <el-form :model="news" :rules="rules" ref="news" label-width="100px" class="demo-ruleForm">
                <el-form-item label="标题" prop="title" style="width: 700px;">
                    <el-input v-model="news.title"></el-input>
                </el-form-item>

                <el-form-item label="内容" prop="content">
                    <el-input type="textarea" v-model="news.content"></el-input>
                </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <span slot="footer" class="dialog-footer">
                    <el-button @click="resetForm('news')">重 置</el-button>
                    <el-button type="primary" @click="submitForm('news', buttonName === '立 即 创 建' ? 'add' : 'update')">{{
                        buttonName }}</el-button>
                </span>
            </div>
        </el-dialog>


    </div>
</template>

<script>
export default {
    name: 'News',
    data() {
        return {
            news: {
                title: '',
                content: '',
            },
            dialogVisible: false,
            //button按钮
            buttonName: '',
            //标题名字
            mytitle: "",
            //输入框内容
            input: '',
            //总数据条数
            counts: 0,
            //当前页
            page: 1,
            //每页条数
            pageSize: 3,
            // 表格数据
            tableData: [],
            rules: {
                title: [
                    { required: true, message: '请输入标题名称', trigger: 'blur' },
                    { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
                ],
                content: [
                    { required: true, message: '请填写资讯内容', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        //输入框查询
        handleQuery() {
            this.getTableData();
        },
        // 打开注册框
        addMemberHandle() {
            this.dialogVisible = true
            this.mytitle = "添加资讯"
            this.buttonName = "立 即 创 建"
            Object.keys(this.news).forEach((key) => (this.news[key] = ''))

        },
        handleSizeChange(val) {
            this.pageSize = val;
            this.page = 1;
            this.getTableData();
        },
        handleCurrentChange(val) {
            this.page = val;
            this.getTableData();
        },
        //获取数据
        getTableData() {
            const params = {
                params: {
                    page: this.page,
                    pageSize: this.pageSize,
                    name: this.input,
                }
            }
            this.axios.get("/news/page", params)
                .then(res => {
                    this.counts = res.data.total;
                    this.tableData = res.data.records;
                })
        },
        //修改发布状态
        statusHandle(val) {
            this.news = val;
            console.log(this.news);
            if (this.news.status == 0) {
                this.news.status = 1;
            } else {
                this.news.status = 0;
            }
            this.axios.post("/news/update", this.news)
                .then(res => {
                    if (res.data === "success") {
                        this.$notify({
                            showClose: true,
                            message: "恭喜你，资讯修改成功！",
                            type: 'success',
                            duration: 1000
                        });
                        this.getTableData();
                    }
                })
        },
        deleteHandle(val) {
            this.$confirm('此操作将永久删除该资讯, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.axios.post("/news/delete", val)
                    .then(res => {
                        if (res.data === "success") {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.getTableData();
                        }
                    })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },
        update(val) {
            this.mytitle = "修改资讯";
            this.dialogVisible = true;
            this.buttonName = "立 即 修 改"
            this.news = val;
        },

        //添加资讯
        submitForm(formName, methodName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    //通过验证
                    if (methodName === "add") {
                        //添加资讯
                        this.axios.post("/news/save", this.news)
                            .then(res => {
                                if (res.data === "success") {
                                    this.$notify({
                                        showClose: true,
                                        message: "恭喜你，资讯添加成功！",
                                        type: 'success',
                                        duration: 1000
                                    });
                                    this.getTableData();
                                    this.dialogVisible = false
                                }
                            })
                    } else if (methodName === "update") {
                        this.axios.post("/news/update", this.news)
                            .then(res => {
                                if (res.data === "success") {
                                    this.$notify({
                                        showClose: true,
                                        message: "恭喜你，资讯修改成功！",
                                        type: 'success',
                                        duration: 1000
                                    });
                                    this.dialogVisible = false;
                                    this.getTableData();
                                }
                            })
                    }
                } else {
                    this.$notify({
                        showClose: true,
                        message: "提交失败，请重新编写！",
                        type: 'error',
                        duration: 1000
                    });
                    this.dialogVisible = false
                }

            });
        },
        //重置表单数据
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    },
    mounted() {
        this.getTableData();
    }
}

</script>
<style scoped>
.News {
    height: 92vh;
    display: flex;
    justify-content: center;
    margin-top: 10px;
}


.tableBar .el-input__icon el-icon-search {
    height: 14px;
}

.nav {
    width: 100%;
    display: flex;
    justify-content: space-between;

}

.tableBar {
    padding-left: 10px;
    background-color: #ffffff;
    height: 600px;
    width: 99.2%;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    padding-left: 10px;
}

.tableBox {
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.2);
}

.pageList {
    margin-top: 25px;
    text-align: center;
}
</style>